home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 121_01 / sdir.c < prev    next >
Text File  |  1985-08-19  |  3KB  |  135 lines

  1. /*
  2. HEADER: CUG 121.??;
  3.  
  4.     TITLE:    Sdir - sorted directory list;
  5.     VERSION:    1.0;
  6.     DATE:    09/01/85;
  7.     DESCRIPTION: "This program produces a sorted directory list on the
  8.         console, with four directory entries per line.";
  9.     KEYWORDS:    sorted, directory;
  10.     SYSTEM:    CP/M;
  11.     FILENAME:    SDIR.C;
  12.     WARNINGS:    "Copyright (c) 1982, Steve Blasingame.
  13.         Requires fcb.h for compile & files.c for link.
  14.         Handles a maximum of 128 files (easily changed).";
  15.     SEE-ALSO:    ZDIR.C (another directory lister);
  16.     AUTHORS:    Steve Blasingame;
  17.     COMPILERS:    BDS-C 1.50;
  18. */
  19.  
  20. #include <bdscio.h>
  21. #include "fcb.h"
  22.  
  23. #define    GLOBMAX    128        /* max dir entries */
  24. #define    FILENAMESIZE    15    /* max size of entry */
  25. char    *globv[GLOBMAX];    /* vector */
  26.  
  27. char    globs[GLOBMAX*FILENAMESIZE]; /* expansion buffer */
  28. int    flag;            /* for lst function */
  29. int    lst();            /* format directory listing */
  30. int    expand();        /* expand global filenames */
  31. int    doglob();        /* parse global file expressions */
  32. int    strcmp();
  33.  
  34. main(argc,argv)
  35. int argc;
  36. char *argv[];
  37. {
  38. char *cp;
  39.  
  40.     argv[argc] = 0;    /* kluge */
  41.     ++argv;
  42.     flag = 0; /* a kluge */
  43.     if (argc > 1) {
  44.         while (*argv != NULL && **argv != NULL) {
  45.             expand(lst,*argv,0);
  46.             argv++;
  47.         }
  48.     }
  49.     else expand(lst,"*.*",0);
  50.  
  51.     putchar('\n');
  52.     exit();
  53. }
  54.  
  55. lst(vector)
  56. char *vector;
  57. {
  58. int q;    /* flag must be an external */
  59.  
  60.     if (flag == 3) {
  61.         puts(vector);
  62.         putchar('\n');
  63.         flag = 0;
  64.     }
  65.     else {
  66.         puts(vector);
  67.         for (q = 0; q != (20-strlen(vector)); q++)
  68.             putchar(' ');
  69.         flag++;
  70.     }
  71.     return 0;
  72. }
  73.  
  74. expand(func,parm1,parm2) /* perform global file expansions */
  75. int (*func)();
  76. char *parm1;
  77. int parm2;
  78. {
  79. int count, ret;
  80.  
  81.     count = GLOBMAX;
  82.  
  83.     setmem(globs,count*FILENAMESIZE,0xff);
  84.     while (count--)
  85.         globv[count] = &globs[count*FILENAMESIZE];
  86.  
  87.     globv[GLOBMAX]=0;
  88.     if ((ret = doglob(parm1)) >= 1) {
  89.         qsort(globs,ret,FILENAMESIZE,strcmp);
  90.         count = 0;
  91.         while (count < ret) {
  92.             if ((*func)(globv[count],parm2) == -1)
  93.                 break;
  94.             count++;
  95.         }
  96.     }
  97.     else {
  98.         puts(parm1);
  99.         puts(": no match\n");
  100.     }
  101.     return 0;
  102. }
  103.  
  104. doglob(string) /* glob - expand filename expressions */
  105. char *string;
  106. {
  107. char f, c, buf[20];
  108. fcb address;
  109. fcb *dirbuf;
  110. int flag, q, i, j;
  111.  
  112.     flag=0;
  113.     i=0;
  114.  
  115.     if (strlen(string) == 2 && *(string+1) == ':') {
  116.         setfcb(address,"????????.???");
  117.         address.f_entry = tolower(*string)-96;
  118.     }
  119.     else setfcb(address,string);
  120.  
  121.     for (f=17; (c=bdos(f,&address)) != 255; f=18) {
  122.         dirbuf = (0x80 + (c * 32));
  123.         dirbuf->f_entry = address.f_entry; 
  124.         pickout(dirbuf, buf);
  125.         j = 0;
  126.         do {
  127.             globv[i][j] = tolower(buf[j]);
  128.         } while (buf[j++]);
  129.         if (++i == GLOBMAX)
  130.             break;
  131.     }
  132.     globv[i] = NULL;    /* null terminate vector */
  133.     return i;
  134. }
  135.